#include "mymousebutton.h"
#include <QPainter>
#include <QPen>
#include <QPixmap>
#include <QTimer>

MyMouseButton::MyMouseButton(QWidget *parent) : QPushButton(parent)
{
    //实例化定时器
    mouseMoveTimer = new QTimer;
    mouseMoveTimer->setInterval(40);
    //设置鼠标移动事件，不需要点击也可触发
    this->setMouseTracking(true);
    //绑定定时器信号
    connect(mouseMoveTimer,&QTimer::timeout,
            this,&MyMouseButton::mouseMoveTimerTimeoutSlot);
}

void MyMouseButton::setMouseType(int type)
{
    //判断老鼠状态
    if(mouseIsActive())
        return;
    //切换状态
    iMouseType = type;
}

void MyMouseButton::mouseActive()
{
    //判断老鼠状态
    if(mouseIsActive())
        return;
    //老鼠向上运动
    iMouseDirection = 0;
    //老鼠从洞内部开始
    iMouseY = this->height();
    //移动速度
    iMouseMoveSpeed = 2;
    //老鼠未被击打
    mouseIsClicked = false;
    //开始移动
    mouseMoveTimer->start();
}

bool MyMouseButton::mouseIsActive()
{
    //老鼠在洞内部，未激活
    if(iMouseY >= this->height())
        return false;
    //定时器未启动，未激活
    if(!mouseMoveTimer->isActive())
        return false;
    return true;
}

int MyMouseButton::getMouseType()
{
    return iMouseType;
}

void MyMouseButton::hitMouse()
{
    //方向向下
    iMouseDirection = 1;
    //速度变快
    iMouseMoveSpeed = 10;
    //标记自己已被击打
    mouseIsClicked = true;
}

void MyMouseButton::mouseHide()
{
    //隐藏老鼠
    iMouseY = this->height();
    this->update();
    //定时器停止
    mouseMoveTimer->stop();
}

void MyMouseButton::mousePause()
{
    mouseMoveTimer->stop();
}

void MyMouseButton::mouseStart()
{
    //老鼠在洞内，不需要启动
    if(iMouseY >= this->height())
        return;
    mouseMoveTimer->start();
}

bool MyMouseButton::getMouseIsClicked()
{
    return mouseIsClicked;
}

void MyMouseButton::mouseMoveTimerTimeoutSlot()
{
    if(iMouseDirection == 0){
        //向上移动
        if(iMouseY - iMouseMoveSpeed < 0){
            //移动到最上方，改变方向
            iMouseDirection = 1;
            return;
        }
        //向上移动一段距离
        iMouseY -= iMouseMoveSpeed;
        //刷新界面
        this->update();
    }
    else
    {
        //向下移动
        if(iMouseY + iMouseMoveSpeed > this->height()){
            //移动到最下方，停止移动
            mouseMoveTimer->stop();
            iMouseY = this->height();
            this->update();
            return;
        }
        //向下移动一段距离
        iMouseY += iMouseMoveSpeed;
        //刷新界面
        this->update();
    }
}

void MyMouseButton::paintEvent(QPaintEvent *)
{
    //画师 QPainter
    //画笔 QPen
    //画布 this
    QPainter painter;
    QPen pen;
    QPixmap pixmap;
    //判断老鼠类型
    switch (iMouseType) {
    case 0:
        pixmap.load(":/images/mouse1.png");
        break;
    case 1:
        pixmap.load(":/images/mouse2.png");
        break;
    case 2:
        pixmap.load(":/images/mouse3.png");
        break;
    case 3:
        pixmap.load(":/images/mouse4.png");
        break;
    }
    //开始绘图
    painter.begin(this);
    //设置画笔
    painter.setPen(pen);
    //绘制图片
    painter.drawPixmap(0,iMouseY,this->width(), this->height(),pixmap);
    //停止绘图
    painter.end();
}

void MyMouseButton::mousePressEvent(QMouseEvent *)
{
    //鼠标指针变成锤子
    //标识按下
    isPress = true;
    this->setCursor(QPixmap(":/images/bt002.png"));
    //发送点击信号
    emit clicked();
}

void MyMouseButton::mouseReleaseEvent(QMouseEvent *)
{
    //鼠标指针变成锤子
    //标识抬起
    isPress = false;
    this->setCursor(QPixmap(":/images/bt001.png"));
}

void MyMouseButton::mouseMoveEvent(QMouseEvent *)
{
    //判断标识，抬起才操作
    if(isPress)
        return;
    //鼠标指针变成锤子
    this->setCursor(QPixmap(":/images/bt001.png"));
}
